package com.chengjue.controller;


import com.chengjue.common.ServerResponse;
import com.chengjue.pojo.TbItem;
import com.chengjue.service.ItemService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * 商品管理controller
 */
@RestController
@RequestMapping("/item")
public class ItemController {
    @Autowired
    ItemService itemService;

    /**
     * 根据商品id查询商品信息
     * @param itemId
     * @return
     */
    @RequestMapping(value = "{itemId}", method = RequestMethod.GET)
    public ServerResponse<TbItem> getItemById(@PathVariable Long itemId){
        TbItem tbItem = itemService.getItmById(itemId);
        if(null != tbItem){
            return  ServerResponse.createBySuccess(tbItem);
        }

        return ServerResponse.createByErrorMessage("没找到相应的商品");
    }

    /**
     * 分页查询商品列表
     * @param pageNo  页数
     * @param pageSize 每页展示的个数
     * @return
     */
    @RequestMapping(value = "list", method = RequestMethod.GET)
    public ServerResponse getItemsList(@RequestParam(required = true,defaultValue = "1") Integer pageNo, @RequestParam(required = true,defaultValue = "20")Integer pageSize){
        return itemService.getItemList(pageNo, pageSize);
    }
}
